package sporteam.web.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

import sporteam.web.client.GeoPtr;
import sporteam.web.client.Track;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

 

public class SendLog extends HttpServlet
{
	private static final Logger MyLog = Logger.getLogger(SendLog.class.getName());
    private static final String SendLogURL = "http://sporteamtest.appspot.com/SendLog";
	private static final long serialVersionUID = 1L;

	@Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException 
    {
		
		resp.setContentType("text/html; charset=UTF-8");
		MyLog.warning("this is for DEBUG only");
		
		String DEBUG_CODE = req.getParameter("debug_code");
		String msg = req.getParameter("msg");
		if(DEBUG_CODE.equals("normal"))
		{
			MyLog.warning(msg);
		}
		else if(DEBUG_CODE.equals("geoptr"))
		{
			String alt = req.getParameter("alt");
			String time = req.getParameter("time");
			String speed = req.getParameter("speed");
			String lat = req.getParameter("lat");
			String longt = req.getParameter("long");

			StringBuilder GeoStr = new StringBuilder();
			GeoStr.append("Recieved Point:");
			GeoStr.append(" [" + lat + "," + longt + "],");
			GeoStr.append(" time= " + time + "," + "altitude=" + alt + ",");
			GeoStr.append(" speed=" + speed);
			GeoStr.append("  ---end point");
			
			MyLog.warning(GeoStr.toString());
		}

		String responseStr = "OK, got msg=" + msg;
		resp.getWriter().write(responseStr);
        resp.getWriter().flush();
    }
	
	   @Override
	   protected void doPost(HttpServletRequest request, HttpServletResponse response)
	           throws ServletException, IOException 
	   {
	       BufferedReader reader = request.getReader();
	       StringBuilder sb = new StringBuilder();
	       String line = reader.readLine();
	       String debug_code;
	       Gson gson = new Gson();
	       MyLog.warning(line);
	       while (line != null) {
	           sb.append(line + "\n");
	           line = reader.readLine();
	       }
	       reader.close();
	       String data = sb.toString();

	       MyLog.warning(data);
	       
	    JSONObject json = null;
		try 
		{
			json = new JSONObject(data);
			debug_code = (String)json.get("debug_code").toString();
			
			if(debug_code.equals("normal_msg"))
			{
				MyLog.warning((String)json.get("msg").toString());
			}
			else if(debug_code.equals("geoptr"))
			{
				String GeoPtrStr = json.get("msg").toString();
				GeoPtr point = gson.fromJson(GeoPtrStr, GeoPtr.class);
				LogPoint(point);
			}
			else if(debug_code.equals("geoptr_lst"))
			{
				Type collectionType = new TypeToken<ArrayList<GeoPtr>>(){}.getType();
				
				String GeoPtrStr = json.get("msg").toString();
				ArrayList<GeoPtr> points = gson.fromJson(GeoPtrStr, collectionType);
				
				int i = 1;
				for(GeoPtr point : points)
				{
				  MyLog.warning("point #" + i++);
				  LogPoint(point);
				}
			}
			else if(debug_code.equals("track"))
			{
				String TrackStr = json.get("msg").toString();
				Track track = gson.fromJson(TrackStr, Track.class);
				LogTrack(track);
			}
			
		}
		catch (JSONException e) 
		{
			e.printStackTrace();
		}
	       
	 
	       
	   }
	   
	   public void LogPoint(GeoPtr point)
	   {
		   StringBuilder GeoStr = new StringBuilder();
			GeoStr.append("Recieved Point:");
			GeoStr.append(" [" + point.getLatitude() + "," + point.getLongitude() + "],");
			GeoStr.append(" time= " + point.getTime() + "," + " time in ms= " + point.getMS());
			GeoStr.append(" speed=" + point.GetSpeed() + "," + " altitude= " + point.getAltitude());
			GeoStr.append("  ---end point");
			
			MyLog.warning(GeoStr.toString());
	   }
	   
	   public void LogTrack(Track track)
	   {
		   
		   StringBuilder TrackStr = new StringBuilder();
		   TrackStr.append("Recieved Track:");
		   TrackStr.append(" track time= " + track.GetTrackTime() + "," + " distance " + track.GetDistance());
		   TrackStr.append(" avg speed=" + track.GetAvgSpeed() + "," + " calories= " + track.GetCalories());
		   MyLog.warning(TrackStr.toString());
			
		
			
			MyLog.warning("  ---end track");
	   }
	   
	
 
}